package com.lg.partition39;

import java.io.*;

/**
 * @author `RKC`
 * @date 2022/5/8 15:20
 */
public class P3842TJOI2007_线段_dp {

    private static final int N = 20010;
    private static int[] l = new int[N], r = new int[N];
    //f[i][0]表示走完第i行且停在第i行的左端点最少使用步数；f[i][1]表示走完第i行且停留在第i行的右端点最少使用步数
    private static int[][] f = new int[N][2];
    private static int n;

    private static StreamTokenizer tokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    private static PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        n = nextInt();
        for (int i = 1; i <= n; i++) {
            l[i] = nextInt();
            r[i] = nextInt();
        }
        f[1][1] = r[1] - 1;
        f[1][0] = r[1] + r[1] - l[1] - 1;
        for (int i = 2; i <= n; i++) {
            int t = r[i] - l[i] + 1;
            f[i][0] = Math.min(f[i - 1][0] + Math.abs(l[i - 1] - r[i]), f[i - 1][1] + Math.abs(r[i - 1] - r[i])) + t;
            f[i][1] = Math.min(f[i - 1][0] + Math.abs(l[i] - l[i - 1]), f[i - 1][1] + Math.abs(r[i - 1] - l[i])) + t;
        }
        writer.println(Math.min(f[n][0] + n - l[n], f[n][1] + n - r[n]));
        writer.flush();
    }

    private static int nextInt() throws IOException {
        tokenizer.nextToken();
        return (int) tokenizer.nval;
    }
}
